package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 好像所有的 shopping 都没有实现  分页 ;  好像又不需要;
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/17 19:47
 */
@Service
public class IHomeServiceImpl implements IHomeService {
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    // 必须在 启动类加上扫描注解;才可以注入;
    @Autowired
    ContentConverter contentConverter;
    // @Autowired
    // PanelContentMapper panelContentMapper;
    @Autowired
    ItemCatMapper itemMapper;

    /**
     * 该方法GET 实现 navigation 请求
     *
     * @return
     */
    @Override
    public NavListResponse navigation() {
        // mapper 与 xml 给定的就是 返回PanelContentItem;而不是返回PanelContent;
        // 所以 PanelContent与 PanelContentItem 是什么关系;
        List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(0);
        System.out.println("panelContentItems = " + panelContentItems);
        // PanelContentItem--->PanelContent进行转化;
        List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
        List<PanelContentDto> panelContentDtos =
                contentConverter.panelContentItem2PanelContentDto(panelContentItemDtos);
        //
        for (PanelContentDto panelContentDto : panelContentDtos) {
            System.out.println("_----------------------------");
            System.out.println("panelContentDto = " + panelContentDto);
            System.out.println("_----------------------------");
        }
        NavListResponse navListResponse = new NavListResponse();
        navListResponse.setPannelContentDtos(panelContentDtos);
        return navListResponse;
    }

    /**
     * 将 sort 写死了, 因为无法判断用户输入的是什么;
     * @return
     */
    @Override
    public CategoryiesReponse categories(String sort) {
        Example example = new Example(ItemCat.class);
        // 声明为Example.OrderBy,     参数tk.mybatis.mapper.entity(实体类)中的字段
        // tk.mybatis.mapper.MapperException: 当前实体类不包含名为sort_order的属性!
        // Example.OrderBy orderBy = example.orderBy("sortOrder"); //
         example.orderBy("sortOrder").desc();
        // 将 sort 变量的值 给到 example对象的sort属性
        // example.createCriteria().andEqualTo("sort", sort);
        // Example.Criteria criteria = example.createCriteria();
        // criteria.
        List<ItemCat> itemCats = itemMapper.selectByExample(example);
        // contentConverter
        List<ProductCateDto> productCateDtos = contentConverter.ItemCats2ProductCateDtos(itemCats);
        // 需要转换;
        CategoryiesReponse categoryiesReponse = new CategoryiesReponse();
        categoryiesReponse.setProductCateDtos(productCateDtos);
        return categoryiesReponse;
    }

    @Override
    public HomePageResponse homepage() {
        //   Panel对象的成员是private List<PanelContentItem> panelContentItems;

        List<Panel> panels = panelMapper.selectPanelContentById1(null); // 查看 sql 语句发现, where条件是 id=null;
        // 现根据 panelId 查询到 一个 list, panels;  随后根据 每一个 panel内的 product_id 查询一个 list:panelContentItems;
        // 最后set
        for (Panel panel : panels) {
            Long productId1 = panel.getProductId();
            if (productId1 == null) {
                continue;
            }
            Integer productId = productId1.intValue();
            // 向 panelContentMapper传递 productId;
            List<PanelContentItem> panelContentItems =
                    panelContentMapper.selectPanelContentAndProductWithPanelId(productId);
            panel.setPanelContentItems(panelContentItems);
        }
        HomePageResponse homePageResponse = new HomePageResponse();
        // 需要做 Panel --> PanelDto对象转换; 即 Panel2Dto 方法
        // 需要做 PanelContentItem --> PanelContentItem2Dto 方法;
        /**
         * 当然,具有List 形式的转化;即
         * PanelContentItem2Dto;
         * PnaelContents2Dto;
         */

        List<PanelContentItem> panelContentItems = null;
        List<PanelContentItemDto> panelContentItemDtos = null;
        List<PanelDto> panelDtos = new ArrayList<PanelDto>();
        //对每一个 panel 对象进行转化为panelDto对象;
        for (Panel panel : panels) {
            PanelDto panelDto = contentConverter.panel2Dto(panel);
            if (panel.getProductId() == null) {
                continue;
            }
            // 根据 panelId 查询 tb_panel_content表;
            panelContentItems =
                    panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId().intValue());
            // 转化一个list;
            panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            panelDto.setPanelContentItems(panelContentItemDtos);
            panelDtos.add(panelDto);
        }

        // 这里方法名字 和 返回值类型  有点令人费解;    同时还需要将 list 转化为 set 对象;
        homePageResponse.setPanelContentItemDtos(new HashSet<>(panelDtos));
        return homePageResponse;
    }
}
